import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_example/config.dart';
import 'package:flutter_example/http/http_manager.dart';
import 'package:flutter_example/http/interceptors/response_interceptor.dart';

class FutureBuilderView extends StatefulWidget {
  @override
  _FutureBuilderViewState createState() => _FutureBuilderViewState();
}

class _FutureBuilderViewState extends State<FutureBuilderView> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("FutureBuilder View"),
      ),
      body: FutureBuilder(
        builder: (context, AsyncSnapshot asyncSnapshot) {
          if (asyncSnapshot.connectionState == ConnectionState.done) {
            return Container(
              child: Center(
                child: Text("${asyncSnapshot.data}"),
              ),
            );
          }
          return Container(
            color: Colors.red,
          );
        },
        future: getFuture(),
      ),
    );
  }

  Future<String> getFuture() async {
    var data = await HttpManager()
        .requestAsync(path: Config.URL_BANNER)
        .catchError((e) {
      print("request fail: " + e.runtimeType.toString());
      if (e is DioError && (e).error is ResponseInterError) {
        String message = ((e).error).message;
        print(message);
      }
    });
    print(data.runtimeType.toString());
    return data;
  }
}
